package com.ellricky.ui.activity

import android.content.Intent
import android.view.View
import android.widget.ImageView
import androidx.viewpager.widget.ViewPager
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ResUtil
import com.andr.common.util.ViewUtil
import com.ellricky.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.ui.adapter.IndexPagerAdapter
import kotlinx.android.synthetic.main.activity_index.*
import kotlinx.android.synthetic.main.layout_index_page.view.*

/**
 * 引导页
 */
class IndexActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private lateinit var views: MutableList<View>
    private lateinit var points: MutableList<ImageView>
    private val icons = mutableListOf(
        R.drawable.icon_index_first, R.drawable.icon_index_second,
        R.drawable.icon_index_third
    )

    private var curPageIndex = 0
    private val PAGE_COUNT = 3


    override fun initUIView() {
        val descArray = ResUtil.getStrArr(this, R.array.IndexPageStr)

        points = mutableListOf()
        views = mutableListOf()

        val count = pointContainer.childCount
        for (j in 0 until count) {
            points.add(j, pointContainer.getChildAt(j) as ImageView)
            views.add(j, ViewUtil.createView(this, R.layout.layout_index_page))
            if (j == count - 1) {
                views[j].btn_enter.setOnClickListener(this)
            } else {
                views[j].btn_enter.visibility = View.GONE
            }
            views[j].tv_desc.text = descArray[j]
            views[j].iv.setImageResource(icons[j])
        }

        points[curPageIndex].isSelected = true
        vp_index.adapter = IndexPagerAdapter(views)

        setListener()
        LocalCacheUtil.setIsFirstUse(false)
    }

    private fun setListener() {
        vp_index.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if (position < 0 || position > points.size - 1 || curPageIndex == position) {
                    return
                }
                points[position].isSelected = true
                points[curPageIndex].isSelected = false
                curPageIndex = position

                val btnEnter = views[curPageIndex].btn_enter
                if (position == points.size - 1) {
                    btnEnter.visibility = View.VISIBLE
                } else {
                    btnEnter.visibility = View.GONE
                }
            }
        })
    }


    override fun getContentLayout(): Int {
        return R.layout.activity_index
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        if (v.id == R.id.btn_enter) {
            jumpLoginPage()
        }

    }

    private fun jumpLoginPage() {
        val intent = Intent(this, LoginActivity::class.java)
        startActivity(intent)
        finish()
    }

}
